/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package millenium3dchess;

/**
 *
 * @author Kenneth
 */
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import millenium3dchess.Game.Color;

public class Pawn extends Chessman {

    public Pawn(Gamevector pos, Game.Color c) throws IOException {
        position = pos;
        color = c;
        cost = 1;
        moveList = new ArrayList<GameSquare>();
        if(c == Color.WHITE){
            icon = ImageIO.read(new File("images\\80x80\\WPawn.png"));
            smallIcon = ImageIO.read(new File("images\\25x25\\WPawn.png"));
        }else{
            icon = ImageIO.read(new File("images\\80x80\\BPawn.png"));
            smallIcon = ImageIO.read(new File("images\\25x25\\BPawn.png"));
        }
    }

    @Override
    public void updateMoveList() {
        moveList.clear();
        //Would like to remove code duplication
        if (color == Game.Color.WHITE) {
            if (!movedYet()) {
                try {
                    if (!Game.getBoard()[position.getX() + 1][position.getY()][position.getZ()].occupied()
                            && !Game.getBoard()[position.getX() + 2][position.getY()][position.getZ()].occupied()) {//move forward 2 sqaures
                        moveList.add(Game.getBoard()[position.getX() + 2][position.getY()][position.getZ()]);
                    }
                } catch (ArrayIndexOutOfBoundsException e) {
                }
                try {
                    if (!Game.getBoard()[position.getX()][position.getY()][position.getZ() + 1].occupied()
                            && !Game.getBoard()[position.getX()][position.getY()][position.getZ() + 2].occupied()) {//move up 2 sqaures
                        moveList.add(Game.getBoard()[position.getX()][position.getY()][position.getZ() + 2]);
                    }
                } catch (ArrayIndexOutOfBoundsException e) {
                }
                try {
                    if (!Game.getBoard()[position.getX() + 1][position.getY()][position.getZ() + 1].occupied()
                            && !Game.getBoard()[position.getX() + 2][position.getY()][position.getZ() + 2].occupied()) {//move forward and up 2 sqaures each
                        moveList.add(Game.getBoard()[position.getX() + 2][position.getY()][position.getZ() + 2]);
                    }
                } catch (ArrayIndexOutOfBoundsException e) {
                }
            }
            //put en passant condition here if()
            try {
                if (!Game.getBoard()[position.getX() + 1][position.getY()][position.getZ()].occupied()) {//move forward
                    moveList.add(Game.getBoard()[position.getX() + 1][position.getY()][position.getZ()]);
                }
            } catch (ArrayIndexOutOfBoundsException e) {
            }
            try {
                if (!Game.getBoard()[position.getX()][position.getY()][position.getZ() - 1].occupied()) {//move down
                    moveList.add(Game.getBoard()[position.getX()][position.getY()][position.getZ() - 1]);
                }
            } catch (ArrayIndexOutOfBoundsException e) {
            }
            try {
                if (!Game.getBoard()[position.getX() + 1][position.getY()][position.getZ() - 1].occupied()) {//move forward and down
                    moveList.add(Game.getBoard()[position.getX() + 1][position.getY()][position.getZ() - 1]);
                }
            } catch (ArrayIndexOutOfBoundsException e) {
            }
            try {
                if (!Game.getBoard()[position.getX()][position.getY()][position.getZ() + 1].occupied()) {//move up
                    moveList.add(Game.getBoard()[position.getX()][position.getY()][position.getZ() + 1]);
                }
            } catch (ArrayIndexOutOfBoundsException e) {
            }
            try {
                if (!Game.getBoard()[position.getX() + 1][position.getY()][position.getZ() + 1].occupied()) {//move up and forward
                    moveList.add(Game.getBoard()[position.getX() + 1][position.getY()][position.getZ() + 1]);
                }
            } catch (ArrayIndexOutOfBoundsException e) {
            }
            //Plus the possible capture scenarios
//            if (!Game.getBoard()[position.getX()][position.getY() + 1][position.getZ()].occupied()) {//move forward and left and capture
//                moveList.add(Game.getBoard()[position.getX()][position.getY() + 1][position.getZ()]);
//            }
        } else { //black starts at the top and moves down
            if (!movedYet()) {
                try {
                    if (!Game.getBoard()[position.getX() - 1][position.getY()][position.getZ()].occupied()
                            && !Game.getBoard()[position.getX() - 2][position.getY()][position.getZ()].occupied()) {//move forward 2 sqaures
                        moveList.add(Game.getBoard()[position.getX() - 2][position.getY()][position.getZ()]);
                    }
                } catch (ArrayIndexOutOfBoundsException e) {
                }
                try {
                    if (!Game.getBoard()[position.getX()][position.getY()][position.getZ() - 1].occupied()
                            && !Game.getBoard()[position.getX()][position.getY()][position.getZ() - 2].occupied()) {//move down 2 sqaures
                        moveList.add(Game.getBoard()[position.getX()][position.getY()][position.getZ() - 2]);
                    }
                } catch (ArrayIndexOutOfBoundsException e) {
                }
                try {
                    if (!Game.getBoard()[position.getX() - 1][position.getY()][position.getZ() - 1].occupied()
                            && !Game.getBoard()[position.getX() - 2][position.getY()][position.getZ() - 2].occupied()) {//move forward and down 2 sqaures each
                        moveList.add(Game.getBoard()[position.getX() - 2][position.getY()][position.getZ() - 2]);
                    }
                } catch (ArrayIndexOutOfBoundsException e) {
                }
            }
            //put en passant condition here if()
            try {
                if (!Game.getBoard()[position.getX() - 1][position.getY()][position.getZ()].occupied()) {//move forward
                    moveList.add(Game.getBoard()[position.getX() - 1][position.getY()][position.getZ()]);
                }
            } catch (ArrayIndexOutOfBoundsException e) {
            }
            try {
                if (!Game.getBoard()[position.getX()][position.getY()][position.getZ() - 1].occupied()) {//move down
                    moveList.add(Game.getBoard()[position.getX()][position.getY()][position.getZ() - 1]);
                }
            } catch (ArrayIndexOutOfBoundsException e) {
            }
            try {
                if (!Game.getBoard()[position.getX() - 1][position.getY()][position.getZ() - 1].occupied()) {//move forward and down
                    moveList.add(Game.getBoard()[position.getX() - 1][position.getY()][position.getZ() - 1]);
                }
            } catch (ArrayIndexOutOfBoundsException e) {
            }
            try {
                if (!Game.getBoard()[position.getX()][position.getY()][position.getZ() + 1].occupied()) {//move up
                    moveList.add(Game.getBoard()[position.getX()][position.getY()][position.getZ() + 1]);
                }
            } catch (ArrayIndexOutOfBoundsException e) {
            }
            try {
                if (!Game.getBoard()[position.getX() - 1][position.getY()][position.getZ() + 1].occupied()) {//move up and forward
                    moveList.add(Game.getBoard()[position.getX() - 1][position.getY()][position.getZ() + 1]);
                }
            } catch (ArrayIndexOutOfBoundsException e) {
            }
        }
    }

    @Override
    public String toString() {
        //White is lowercase while black is uppercase
        if (color == Game.Color.WHITE) {
            return "p ";
        }
        return "P ";
    }
}
